package cn.liuhuan1.blogsModel.service;

import cn.liuhuan1.blogsModel.dao.LeaveWordDao;
import cn.liuhuan1.entity.PageResult;
import cn.liuhuan1.webModelBean.api.LeaveWordImp;
import cn.liuhuan1.webModelBean.bo.LeaveWordBo;
import cn.liuhuan1.webModelBean.vo.LeaveWordVo;
import cn.liuhuan1.webModelBean.vo.WebUserVo;
import com.mysql.cj.util.StringUtils;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class LeaveWordService implements LeaveWordImp {
    @Autowired
    public LeaveWordDao leaveWordDao;

    @Autowired
    private WebUserService webUserService;

    @Override
    public List<LeaveWordVo> getLeaveWord(){
        List<LeaveWordVo> leaveWordVoList = leaveWordDao.getLeaveWord();
        return leaveWordVoList;
    }

    @Override
    public PageResult<LeaveWordVo> getPageLeaveWord(LeaveWordBo leaveWordBo) {
        if(leaveWordBo.getPageCurrent() < 1){
            leaveWordBo.setPageCurrent(0);
        }else{
            leaveWordBo.setPageCurrent(leaveWordBo.getPageCurrent() - 1);
        }
        if(leaveWordBo.getPageSize() < 9){
            leaveWordBo.setPageCurrent(10);
        }

        PageResult<LeaveWordVo> pageResult = new PageResult<>();

        List<LeaveWordVo> leaveWordVoList = leaveWordDao.getPageLeaveWord(leaveWordBo);

        int count = leaveWordDao.getPageCount(leaveWordBo);

        pageResult.setPageList(leaveWordVoList);

        pageResult.setPageShowTotal(count);
        pageResult.setPageCurrent(leaveWordBo.getPageCurrent());
        pageResult.setPageSize(leaveWordBo.getPageSize());
        return pageResult;
    }

    @Override
    public LeaveWordVo getLeaveWordById(String id) {
        if(id != null && id.trim() != ""){
            LeaveWordVo leaveWordVo = leaveWordDao.getLeaveWordById(id);
            if(leaveWordVo != null){
                return leaveWordVo;
            }
        }
        throw new NullPointerException();
    }

    @Override
    @Transactional
    public void saveLeaveWord(LeaveWordBo leaveWordBo) {
        if(leaveWordBo.getWebUserId() == null || leaveWordBo.getWebUserId().trim() == ""){
            throw new NullPointerException("用户未登录");
        }
        WebUserVo webUserVo = webUserService.getById(leaveWordBo.getWebUserId());
        if(webUserVo == null){
            throw new NullPointerException("用户未登录");
        }
        leaveWordBo.setWebUserName(webUserVo.getName());
        leaveWordDao.saveLeaveWord(leaveWordBo);
    }

    @Override
    public void updateLeaveWord(LeaveWordBo leaveWordBo) {
        leaveWordDao.updateLeaveWord(leaveWordBo);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delLeaveWord(String id) {
        if(id != null && id.trim() != ""){
            leaveWordDao.delLeaveWord(id);
        }else{
            throw new NullPointerException();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(String status,String id){
        if (status == null || status.trim() == "" || id == null || id.trim() == ""){
            throw new NullPointerException();
        }
        leaveWordDao.updateStatus(status,id);
    }

}
